package com.yuyou.train.lesson.service.impl;

import java.util.Date;
import java.util.List;

import com.yuyou.train.util.enums.lesson.QuestionEnums;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.lesson.mapper.ResourceExamMapper;
import com.yuyou.train.lesson.service.ILessonResourceRuleRelationService;
import com.yuyou.train.lesson.service.ILessonService;
import com.yuyou.train.lesson.service.IResourceExamItemService;
import com.yuyou.train.lesson.service.IResourceExamService;
import com.yuyou.train.lesson.service.IResourceService;
import com.yuyou.train.service.api.lesson.dto.ExamDetailDTO;
import com.yuyou.train.service.api.lesson.dto.ExamPageDTO;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleRelation;
import com.yuyou.train.service.api.lesson.entity.Resource;
import com.yuyou.train.service.api.lesson.entity.ResourceExam;
import com.yuyou.train.service.api.lesson.entity.ResourceExamItem;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.enums.CommEnums;
import com.yuyou.train.util.message.lesson.ExamMess;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class ResourceExamServiceImpl implements IResourceExamService{
 
	@Autowired
	private ResourceExamMapper resourceExamMapper;
	
	@Autowired
	private IResourceExamItemService resourceExamItemService;
	
	@Autowired
	private IResourceService resourceService;
	
	@Autowired
	private ILessonService lessonService;
	
	@Autowired
	private ILessonResourceRuleRelationService lesResRuleRelService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return resourceExamMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(ResourceExam record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return resourceExamMapper.insert(record);
	}
	
	@Override
	public ResBoolSimpleInfo<String> addExam(ResourceExam record) {
		record.setIsUse(CommEnums.isOrNot.no.getValue());
		this.insert(record);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResBoolSimpleInfo<String> updateExam(ResourceExam resourceExam) {

		//试卷绑定课程不能修改
		ResBoolSimpleInfo<String> res=hasLessonRel(resourceExam.getId());
		if(!res.isSuccess()){
			return res;
		}
		
		ResourceExam resourceExamQuery = selectByPrimaryKey(resourceExam.getId());
		if(CommEnums.isUse.yes.getValue().equals(resourceExamQuery.getIsUse())){
			return new ResBoolSimpleInfo<String>(false, ExamMess.ERR_UPDATE_EXAM_USE);
		}
		resourceExam.setUpdateTime(DateUtil.now());
		resourceExamMapper.updateByPrimaryKeySelective(resourceExam);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResourceExam selectByPrimaryKey(String id) {
		return resourceExamMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(ResourceExam record) {
		record.setUpdateTime(new Date());
		return resourceExamMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ResBoolSimpleInfo<String> delExam(String id) {	
		
		//试卷绑定课程不能修改
		ResBoolSimpleInfo<String> res=hasLessonRel(id);
		if(!res.isSuccess()){
			return res;
		}
		//试卷启动状态不能删除
		ResourceExam resourceExam = this.selectByPrimaryKey(id);
		if(resourceExam.getIsUse().equals(CommEnums.isOrNot.yes.getValue())){
			return new ResBoolSimpleInfo<String>(false,ExamMess.ERR_DEL_EXAM_USE);
		}	
		//有明细关联不能删除
		List<ResourceExamItem> examItemList=resourceExamItemService.findExamItemByExamId(id);
		if(examItemList.size()>0){
			return new ResBoolSimpleInfo<String>(false,ExamMess.ERR_DEL_EXAM_HAS_ITEM);
		}		
		//资源关联不能删除
		List<Resource> resourceList=resourceService.findResourceByExamId(id);
		if(resourceList.size()>0){
			return new ResBoolSimpleInfo<String>(false,ExamMess.ERR_DEL_EXAM_HAS_RESOURCE);
		}
		this.deleteByPrimaryKey(id);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public List<ExamPageDTO> findExamPageList(Pagination<ExamPageDTO> p) {
		return resourceExamMapper.findExamPageList(p);
	}

	@Override
	public int findExamPageCount(Pagination<ExamPageDTO> p) {
		return resourceExamMapper.findExamPageCount(p);
	}

	@Override
	public ResBoolSimpleInfo<String> batchUpdateStatus(List<String> ids, String isUse) {

		for(String examId : ids){
			
			//试卷绑定课程不能修改
			ResBoolSimpleInfo<String> res=hasLessonRel(examId);
			if(!res.isSuccess()){
				return res;
			}
			
			ResourceExam resourceExam = selectByPrimaryKey(examId);
			List<ResourceExamItem> examItemList = resourceExamItemService.findExamItemByExamId(examId);
			int questionTotal = 0; // 总题目数
			int scoreTotal = 0; // 总分数
			for(ResourceExamItem resourceExamItem : examItemList){
				questionTotal++ ;
				scoreTotal += resourceExamItem.getScore();
			}
			if("1".equals(isUse) && questionTotal != resourceExam.getNum()){
				return new ResBoolSimpleInfo<String>(false, resourceExam.getName() + "的"+ ExamMess.ERR_EXAM_USE_QUESTION_NOT_MATCH);
			}
			if("1".equals(isUse) && scoreTotal != resourceExam.getScore()){
				return new ResBoolSimpleInfo<String>(false, resourceExam.getName() + "的" +ExamMess.ERR_EXAM_USE_SCORE_NOT_MATCH);
			}
		}
		resourceExamMapper.batchUpdateStatus(ids, isUse);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ExamDetailDTO selectDetailByPrimaryKey(String id) {
		return resourceExamMapper.selectDetailByPrimaryKey(id);
	}
	
//	public ResBoolSimpleInfo<String> isLessonUse(String id){
//		ResBoolSimpleInfo<String> res = new ResBoolSimpleInfo<>(true, null);
//		List<Resource> resList=resourceService.findResourceByExamId(id);
//		for(Resource r:resList ){
//			res=resourceService.isLessonUse(r.getId(), null);
//			if(!res.isSuccess()){
//				return res;
//			}
//		}
//		return ResBoolSimpleInfo.getSuccessInfo();
//	}
	
	@Override
	public ResBoolSimpleInfo<String> hasLessonRel(String id){
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		List<Resource> resList=resourceService.findResourceByExamId(id);
		for(Resource r:resList ){
			res=resourceService.hasLessonRel(r.getId());
			if(!res.isSuccess()){
				return res;
			}
		}
		return ResBoolSimpleInfo.getSuccessInfo();
	}

}